<template>
  <div class="reporting">
    <repo-nav @currentNav="currentNav" :currentIndex="currentIndex"></repo-nav>
    <!-- 列表 -->
    <repo-list :noMoreData="noMoreData" :pectionList="pectionList"></repo-list>
    <!-- 上报事件 -->
    <report></report>
  </div>
</template>

<script>
import RepoNav from "./child/RepoNav";
import RepoList from "./child/RepoList";
import { getInspectionList, authLogin } from "api/reporting";
import { throtTling } from "utils/throttling.js";
import { getScrollTop, getScrollHeight, getWindowHeight } from "utils/scroll";

export default {
  name: "Reporting",
  components: {
    RepoNav,
    RepoList,
  },
  data() {
    return {
      currentIndex: "0",
      stats: 10,
      pectionList: [],
      page: 1,
      limit: 5,
      // 没有更多数据
      noMoreData: false,
      // openid
      openid: null,
    };
  },
  mounted() {
    if (localStorage.getItem("openid")) {
      this.openid = localStorage.getItem("openid");
    }
    this.$nextTick(() => {
      window.addEventListener("scroll", this.menu);
    });
    this.getInspectionList();
  },
  methods: {
    getInspectionList() {
      let that = this;
      if (that.openid) {
        let data = {
          openid: that.openid,
          stats: that.stats,
          page: that.page,
          limit: that.limit,
        };
        getInspectionList(data).then((res) => {
          if (res.code == 200) {
            that.pectionList.push(...res.data.data);
            if (res.data.data.length <= 0) {
              that.noMoreData = true;
            }
          } else {
            this.$toast({
              message: res.msg,
            });
          }
        });
      }
    },

    menu: throtTling(function () {
      if (!this.noMoreData) {
        // 此时还有剩余数据未加载完
        let scroll = getScrollTop() + getWindowHeight() - getScrollHeight();
        if (scroll > -400) {
          //400：距离底部多少像素开始触发
          this.page += 1;
          this.getInspectionList();
        }
      }
    }, 600),

    currentNav(index) {
      // 切换后初始化数据
      this.page = 1;
      this.pectionList = [];
      this.noMoreData = false;
      this.currentIndex = index;
      if (index == 0) {
        this.stats = 10;
      } else if (index == 1) {
        this.stats = 0;
      } else {
        this.stats = 4;
      }
      this.getInspectionList();
    },
  },
  //离开删除绑定事件
  beforeDestroy() {
    this.page = 1;
    this.noMoreData = false;
    window.removeEventListener("scroll", this.menu);
  },
};
</script>

<style lang="less" scoped>
</style>